package com.link.base.base.appinfo.controller;

import com.link.base.base.appinfo.model.AppLogin;
import com.link.base.base.appinfo.service.AppLoginService;
import com.link.base.user.model.CoreUser;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.cllog.LCLogger;
import com.link.core.util.DateUtil;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * APP登录日志
 *
 * @author ouyangzhenqiang
 * @date 2018/10/16
 */
@Controller
@RequestMapping("/link/appLogin")
public class AppLoginController extends BasicController<AppLogin> {

    @Resource
    private AppLoginService appLoginService;

    @Resource
    private KeyGenerateService keyGenerateService;

    @Override
    public BasicService<AppLogin> getBasicService() throws Exception {
        return appLoginService;
    }

    /**
     * 登录日志
     *
     * @author ouyangzhenqiang
     * @date 2018/10/16
     */
    @PostMapping(value = "/createLoginLog", produces = "application/json", name = "移动端登录日志")
    @ResponseBody
    public Map<String, Object> createLoginLog(@RequestBody AppLogin loginLog, HttpSession session,
                                              HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        String unknown = "unknown";
        try {
            String ip = request.getHeader("x-forwarded-for");
            if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
                ip = request.getHeader("Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
                ip = request.getHeader("WL-Proxy-Client-IP");
            }
            if (ip == null || ip.length() == 0 || unknown.equalsIgnoreCase(ip)) {
                ip = request.getRemoteAddr();
            }
            CoreUser user = UserUtil.getUser();
            Date date = new Date();
            loginLog.setId(keyGenerateService.keyGenerate());
            loginLog.setUserName(user.getFirstName());
            loginLog.setUserId(user.getId());
            loginLog.setLoginTime(date);
            loginLog.setLogoutTime(date);
            loginLog.setSessionId(session.getId());
            loginLog.setIpAddress(ip);
            loginLog.setLastUpdated(DateUtil.dateToStr(date, "yyyy-MM-dd HH:mm:ss"));
            loginLog.setCreated(DateUtil.dateToStr(date, "yyyy-MM-dd HH:mm:ss"));
            loginLog.setCreatedBy(user.getId());
            loginLog.setLastUpdatedBy(user.getId());
            loginLog.setCorpId(user.getCorpid());
            appLoginService.create(loginLog);
            result.put("success", true);
            result.put("newRow", loginLog);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("message", e.toString());
        }
        return result;
    }
}
